package com.fdb.efp.esb.service.impl.sed.tax;

import java.io.File;
import java.util.List;
import java.util.Optional;

import com.fdb.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.basic.framework.core.util.DateUtility;
import com.fdb.efp.esb.service.bo.req.sed.tax.ReqSedTaxFileInformBean;
import com.fdb.efp.esb.service.bo.resp.sed.tax.RespSedTaxFileInformBean;
import com.fdb.efp.esb.service.client.ESBClientFactory;
import com.fdb.efp.esb.service.facade.sed.tax.SedTaxFileInformService;

@Service("sedTaxFileInformService")
public class SedTaxFileInformServiceImpl implements SedTaxFileInformService {

	//日志记录器
	private static Logger logger = LoggerFactory.getLogger(SedTaxFileInformServiceImpl.class);
	
	/**
	 * 需求编号：2019D0519<br/>
	 * 问题编号：<br/>
	 * 功能描述：通知税局进行税局文件的回写<br/>
	 * 开发人员：周才顺<br/>
	 * 创建时间：2019年5月14日 上午9:07:48<br/>
	 * @author zhoucs1
	 * @since  1.0 
	 * @version 1.0
	 * @param reqSedTaxFileInform
	 * @return
	 * @throws Exception 
	 */
	@Override
	public boolean informTaxBureau(String subs_data_date) throws Exception{
		String date_8 = DateUtility.format10To8(subs_data_date);
		ReqSedTaxFileInformBean reqSedTaxFileInform = new ReqSedTaxFileInformBean();
		reqSedTaxFileInform.setFinInst("31302");
		reqSedTaxFileInform.setTaxFilePath("" + File.separator + date_8 + File.separator + "WJ0131302"+date_8+"00.dat"); //待定
		reqSedTaxFileInform.setTxnCd("WD80"); //待定
		logger.info("调用税局文件回写通知接口开始，请求参数：" + JSONObject.toJSONString(reqSedTaxFileInform));
		RespSedTaxFileInformBean respSedTaxFileInformBean = null;
		try {
			EsbRespServiceBean esbRespServiceBean = ESBClientFactory.buildClient()
					.withBody(reqSedTaxFileInform)
					.withTradeNo(ESBConstant.TRADE_CODE_50130003)
					.withScene(ESBConstant.SCENE_NO_04)
					.create()
					.sendAndReceive();
			Optional.ofNullable(esbRespServiceBean).orElseThrow(() -> new ESBException("访问ESB发生异常，请检查请求参数是否正确或ESB是否连通"));

			// 处理开放平台返回信息
			List<EsbRespRetInfBean> retInfArry = esbRespServiceBean.getSysHead().getRetInfArry();
			if(!"000000".equals(retInfArry.get(0).getRetCd())) {
				throw new  ESBException("开放平台发生异常，返回信息为" + retInfArry.toString());
			}
					
			respSedTaxFileInformBean = esbRespServiceBean.getBody(RespSedTaxFileInformBean.class);
			
			
			logger.info("调用税局文件回写通知接口结束，返回信息：" + JSONObject.toJSONString(respSedTaxFileInformBean));
		} catch (ESBException e) {
			logger.error("调用税局文件回写通知接口出现异常：" + e.getMessage());
			throw new ESBException("调用税局文件回写通知接口出现异常：" + e.getMessage());
		}
		return respSedTaxFileInformBean != null && "0".equals(respSedTaxFileInformBean.getRetCd()); //成功返回，并且处理结果为成功-0
	}

}
